package com.choosefine.it.team.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.IdsUtil;
import com.choosefine.it.common.utils.IntegerUtil;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.team.model.TeamFindProjectInfo;
import com.choosefine.it.team.model.dto.TeamFindProjectUpdateDTO;
import com.choosefine.it.team.model.po.TeamFindProjectPO;
import com.choosefine.it.team.service.TeamFindProjectInfoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/team/TeamFindProjectInfo")
@Api(value = "班组发布找项目以及管理相关", description = "潘钱钦")
public class TeamFindProjectInfoController extends BaseController {
	Logger logger = LoggerFactory.getLogger(TeamFindProjectInfoController.class);
	
	@Autowired
	private TeamFindProjectInfoService teamFindProjectInfoService;
	@Autowired
	CustomRestTemplate restTemplate;
	@Autowired
	ObjectMapper jsonObjectMapper;
	@Autowired
	MongoTemplate mongoTemplate;

	/**
	 * 发布找项目信息
	 * 
	 * @param teamFindProjectInfoPO
	 * @return
	 * @throws JsonProcessingException
	 */
	@ApiOperation("添加找项目信息")
	@RequestMapping(method = RequestMethod.POST, value = "/publish", produces = "application/json")
	public String publish(@RequestBody TeamFindProjectInfo teamFindProjectInfo)
			throws JsonProcessingException, BusinessException {
		String result = null;
		if ( teamFindProjectInfo!=null&&StringUtils.isNotBlank(teamFindProjectInfo.getUserCode())) {
			long isSuccess = teamFindProjectInfoService.addTeamFindProjectInfo(teamFindProjectInfo);
			if (isSuccess > 0) {
				result = responseSuccess(isSuccess);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_CREATE_ERROR);
			}
		} else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据userCode查询该用户的找项目信息列表
	 * 
	 * @param userCode
	 * @return
	 */
	@ApiOperation(value = "获取班组发布找工作的信息列表", notes = "根据url的userCode和需要分页的参数来查询班组发布找工作的信息列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "用户的唯一编号", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "verifyStatus", value = "审核状态", required = true, dataType = "Integer", paramType = "path")})
	@RequestMapping(method = RequestMethod.GET, value = "/getTeamRecruitmentInfoList/{userCode}/{pageNum}/{pageSize}/{verifyStatus}", produces = "application/json")
	public String getTeamFindProjectInfoList(@PathVariable String userCode, @PathVariable Integer pageNum,
			@PathVariable Integer pageSize, @PathVariable Integer verifyStatus) throws BusinessException {
		String result = null;
		PageDTO page = new PageDTO();
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		Map<String, Object> params = new HashMap<>();
		params.put("userCode", userCode);
		params.put("verifyStatus", verifyStatus);
		Page<TeamFindProjectInfo> list = teamFindProjectInfoService.getTeamFindProjectInfoList(params, page);
		if (list.size() > 0) {
			result = responseSuccess(list);
		} else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
		}
		return result;

	}

	/**
	 * 根据id修改某条找项目信息的状态
	 * 
	 * @param
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "修改某条发布找工作信息的发布状态", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "publishStatus", value = "发布状态0开始找1停止找", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "id", value = "该条记录的id", required = true, dataType = "Integer", paramType = "path") })
	@RequestMapping(method = RequestMethod.PUT, value = "/updatePublishstatus/{publishStatus}/{id}", produces = "application/json")
	public String updateFindProjectInfoPublishstatusByID(@PathVariable Integer publishStatus,
			@PathVariable Integer id) {
		String result = null;
		if (IntegerUtil.isNotNull(publishStatus) && IntegerUtil.isNotZero(id)) {
			Map<String, Object> params = new HashMap<>();
			params.put("publishStatus", publishStatus);// 发布状态（1开始招聘2停止招聘）
			params.put("id", id);
			params.put("utime", DateUtils.getNowTime());
			Integer num = teamFindProjectInfoService.updateFindProjectInfoPublishstatusByID(params);
			if (num.intValue() > 0) {
				result = responseSuccess(num, ResultCode.SUCCESS, ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_UPDATE_FAILD);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据id软删除某条找项目信息
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "软删除某条发布找工作信息", notes = "")
	@ApiImplicitParam(name = "id", value = "该条记录的id", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(method = RequestMethod.PUT, value = "/deleteProjectCollection/{id}", produces = "application/json")
	public String deleteProjectCollection(@PathVariable Integer id) {
		String result = null;
		if (IntegerUtil.isNotZero(id)) {
			Map<String, Object> params = new HashMap<>();
			params.put("id", id);
			params.put("isValid", CommonConstant.INVALID);
			params.put("utime", DateUtils.getNowTime());
			Integer num = teamFindProjectInfoService.deleteTeamFindProjectInfo(params);
			if (num.intValue() > 0) {
				result = responseSuccess(num, ResultCode.SUCCESS, ResultCode.MSG_DELETE_SUCCESS);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_DELETE_FAILD);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}

	/**
	 * 根据id查看某条发布信息的详情
	 * 
	 * @author panqianqin
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "获取某条找项目信息的详情", notes = "根据id获取某条找项目信息的详情")
	@ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/getTeamFindProjectInfoDetail/{id}", method = RequestMethod.GET)
	public String getTeamFindProjectInfoDetail(@PathVariable Integer id) {
		String result = null;
		if (IntegerUtil.isNotZero(id)) {
			TeamFindProjectInfo t = teamFindProjectInfoService.getTeamFindProjectInfoDetail(id);
			if (t != null) {
				result = responseSuccess(t, ResultCode.SUCCESS, ResultCode.MSG_QUERY_SUCCESS);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}

	/**
	 * 更新某条信息详情
	 * 
	 * @param teamFindProjectInfo
	 * @return
	 * @throws JsonProcessingException
	 */
	@ApiOperation(value = "更新班组发布找项目的信息", notes = "")
	@RequestMapping(value = "/updateTeamFindProjectInfo", method = RequestMethod.PUT)
	public String updateTeamFindProjectInfo(@RequestBody TeamFindProjectInfo teamFindProjectInfo)
			throws JsonProcessingException {
		String result = null;
		if (teamFindProjectInfo!=null&&StringUtils.isNotBlank(teamFindProjectInfo.getUserCode())) {
			Integer isSuccess = teamFindProjectInfoService.updateTeamFindProjectInfo(teamFindProjectInfo);
			if (isSuccess.intValue() > 0) {
				result = responseSuccess(isSuccess, ResultCode.SUCCESS, ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_UPDATE_FAILD);
			}
		}else{
			result = responseFail(ResultCode.FAILED,ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}

	@ApiIgnore
	@RequestMapping(value = "/updateTeamFindProjectVerifyStatus", method = RequestMethod.PUT)
	public String updateTeamFindProjectVerifyStatus(@RequestBody TeamFindProjectUpdateDTO teamFindProjectUpdate) throws Exception{
		String result = null;
		Map<String, Object> params = new HashMap<>();
		if (IntegerUtil.isNotNull(teamFindProjectUpdate.getId()) && IntegerUtil.isNotNull(teamFindProjectUpdate.getVerifyStatus())) {
			params.put("id", teamFindProjectUpdate.getId());
			params.put("verifyStatus", teamFindProjectUpdate.getVerifyStatus());
			params.put("utime", DateUtils.getNowTime());
			if(teamFindProjectUpdate.getReason() != null && !"".equals(teamFindProjectUpdate.getReason())){
				params.put("reason", teamFindProjectUpdate.getReason());
			}
			if(teamFindProjectUpdate.getPublisher() != null && !"".equals(teamFindProjectUpdate.getPublisher())){
				params.put("publisher", teamFindProjectUpdate.getPublisher());
			}
			int isSuccess = teamFindProjectInfoService.updateFindProjectVerifyStatus(params);
			if (isSuccess > 0) {
				result = responseSuccess(isSuccess, ResultCode.MSG_UPDATE_SUCCESS);
			}else {
				result = responseFail(ResultCode.MSG_UPDATE_FAILD);
			}
		} else responseFail(ResultCode.MSG_PARAMS_ERROR);
		return result;
	}

	@ApiIgnore
	@RequestMapping(value = "/deleteTeamFindProject/{idss}", method = RequestMethod.GET)
	public String deleteTeamFindProject(@PathVariable String idss) throws Exception{
		List<Integer> ids = new ArrayList<>();
		String result = null;
		Map<String, Object> params = new HashMap<>();
		ids=IdsUtil.getIds(idss);
		if (ids.size() > 0 ) {
			params.put("ids", ids);
			params.put("utime", DateUtils.getNowTime());
			int isSuccess = teamFindProjectInfoService.deleteFindProject(params);
			if (isSuccess == ids.size()) {
				result = responseSuccess(isSuccess, ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_UPDATE_FAILD);
			}
		} else {
			responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}

	@ApiIgnore
	@RequestMapping(value = "/selectFindProjectInfoByCondition", method = RequestMethod.POST)
	public String selectFindProjectInfoByCondition(@RequestBody TeamFindProjectPO teamFindProjectPo) {
		String result = null;
		PageDTO page = new PageDTO();
		page.setPageNum(teamFindProjectPo.getPageNum());
		page.setPageSize(teamFindProjectPo.getPageSize());
		Map<String, Object> params = new HashMap<>();
		if (teamFindProjectPo.getEndTime() != null) {
			params.put("endTime", teamFindProjectPo.getEndTime());
		}
		if (teamFindProjectPo.getStartTime() != null) {
			params.put("startTime", teamFindProjectPo.getStartTime());
		}
		if (teamFindProjectPo.getTeamTypeId() != null) {
			params.put("teamTypeId", teamFindProjectPo.getTeamTypeId());
		}
		if (teamFindProjectPo.getTeamTypeName() != null) {
			params.put("teamTypeName", teamFindProjectPo.getTeamTypeName());
		}
		if (teamFindProjectPo.getUserCode() != null && !"".equals(teamFindProjectPo.getUserCode())) {
			params.put("userCode", teamFindProjectPo.getUserCode());
		}
		if (teamFindProjectPo.getVerifyStatus() != null) {
			params.put("verifyStatus", teamFindProjectPo.getVerifyStatus());
		}
		
		Page<TeamFindProjectInfo> list = teamFindProjectInfoService.selectFindProjectInfoByCondition(params, page);
		result = responseSuccess(list.toPageInfo());
		return result;

	}
}
